// 导入表单验证插件
// const app = getApp()
import wxValidate from 'wechat-validate'
Page({
  data: {
    mobile: '',
    password:'',
    password1:''
  },
  // 将插件注入到页面实例中
  behaviors: [wxValidate],
  rules: {
    mobile: [
      {required: true, message: '请填写手机号码!'},
      {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'}
    ],
    password:[
      {required: true, message: '请填写密码!'},
      {pattern: /^\w{6}$/, message: '请填写正确密码!'}
    ]
  },
  valite(){
     const {message,valid} = this.validate('mobile')
     if(!valid) return wx.utils.toast(message)

  },
   
  async formSubmit(){
    // console.log(1);
    if(this.data.password !== this.data.password1){
      return wx.utils.toast('两次密码不一致')
  }
    console.log(this.validate());
    if(!this.validate()) return wx.utils.toast('注册失败')
    // console.log(2);
  const res = await wx.http.post('/index/reg',{
    mobile:this.data.mobile,
    password:this.data.password
 })
     console.log(res);
     if(res.errno !== 0) return wx.utils.toast('注册失败,请重新注册')

  // app.setToken("token",res.data.token)
  wx.utils.toast('注册成功,即将跳转登录页')
  wx.navigateTo({
    url: '../login/login',
  });
    
  }
})